/**
 * Copyright (C) 2013-2014 EaseMob Technologies. All rights reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *     http://www.apache.org/licenses/LICENSE-2.0
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.hengtiansoft.lfy.chart.applib.utils;

import android.content.Context;
import android.content.SharedPreferences;

public class HXPreferenceUtils {
    /**
     * 保存Preference的name
     */
    public static final String PREFERENCE_NAME = "saveInfo";
    private static SharedPreferences mSharedPreferences;
    private static HXPreferenceUtils mPreferenceUtils;
    private static SharedPreferences.Editor editor;

    private String SHARED_KEY_SETTING_NOTIFICATION = "shared_key_setting_notification";
    private String SHARED_KEY_SETTING_SOUND = "shared_key_setting_sound";
    private String SHARED_KEY_SETTING_VIBRATE = "shared_key_setting_vibrate";
    private String SHARED_KEY_SETTING_SPEAKER = "shared_key_setting_speaker";

    private static String SHARED_KEY_SETTING_CHATROOM_OWNER_LEAVE = "shared_key_setting_chatroom_owner_leave";
    private static String SHARED_KEY_SETTING_GROUPS_SYNCED = "SHARED_KEY_SETTING_GROUPS_SYNCED";
    private static String SHARED_KEY_SETTING_CONTACT_SYNCED = "SHARED_KEY_SETTING_CONTACT_SYNCED";
    private static String SHARED_KEY_SETTING_BALCKLIST_SYNCED = "SHARED_KEY_SETTING_BALCKLIST_SYNCED";

    private static String SHARED_KEY_CURRENTUSER_NICK = "SHARED_KEY_CURRENTUSER_NICK";
    private static String SHARED_KEY_CURRENTUSER_AVATAR = "SHARED_KEY_CURRENTUSER_AVATAR";

    private static String ONE = "ONE";
    private static String TWO = "TWO";
    private static String THREE = "THREE";

    private static String DOWNLOADSERVICE_SUCCESS = "DOWNLOADSERVICE_SUCCESS";
    private static String DOWNLOADSERVICE_PATH = "DOWNLOADSERVICE_PATH";

    private HXPreferenceUtils(Context cxt) {
        mSharedPreferences = cxt.getSharedPreferences(PREFERENCE_NAME,
                Context.MODE_PRIVATE);
        editor = mSharedPreferences.edit();
    }

    public static synchronized void init(Context cxt) {
        if (mPreferenceUtils == null) {
            mPreferenceUtils = new HXPreferenceUtils(cxt);
        }
    }

    /**
     * 单例模式，获取instance实例
     * 
     * @param cxt
     * @return
     */
    public static HXPreferenceUtils getInstance() {
        if (mPreferenceUtils == null) {
            throw new RuntimeException("please init first!");
        }

        return mPreferenceUtils;
    }

    public void setSettingMsgNotification(boolean paramBoolean) {
        editor.putBoolean(SHARED_KEY_SETTING_NOTIFICATION, paramBoolean);
        editor.commit();
    }

    public boolean getSettingMsgNotification() {
        return mSharedPreferences.getBoolean(SHARED_KEY_SETTING_NOTIFICATION,
                true);
    }

    public void setSettingMsgSound(boolean paramBoolean) {
        editor.putBoolean(SHARED_KEY_SETTING_SOUND, paramBoolean);
        editor.commit();
    }

    public boolean getSettingMsgSound() {

        return mSharedPreferences.getBoolean(SHARED_KEY_SETTING_SOUND, true);
    }

    public void setSettingMsgVibrate(boolean paramBoolean) {
        editor.putBoolean(SHARED_KEY_SETTING_VIBRATE, paramBoolean);
        editor.commit();
    }

    public boolean getSettingMsgVibrate() {
        return mSharedPreferences.getBoolean(SHARED_KEY_SETTING_VIBRATE, true);
    }

    public void setSettingMsgSpeaker(boolean paramBoolean) {
        editor.putBoolean(SHARED_KEY_SETTING_SPEAKER, paramBoolean);
        editor.commit();
    }

    public boolean getSettingMsgSpeaker() {
        return mSharedPreferences.getBoolean(SHARED_KEY_SETTING_SPEAKER, true);
    }

    public void setSettingAllowChatroomOwnerLeave(boolean value) {
        editor.putBoolean(SHARED_KEY_SETTING_CHATROOM_OWNER_LEAVE, value);
        editor.commit();
    }

    public boolean getSettingAllowChatroomOwnerLeave() {
        return mSharedPreferences
                .getBoolean(SHARED_KEY_SETTING_CHATROOM_OWNER_LEAVE, true);
    }

    public void setGroupsSynced(boolean synced) {
        editor.putBoolean(SHARED_KEY_SETTING_GROUPS_SYNCED, synced);
        editor.commit();
    }

    public boolean isGroupsSynced() {
        return mSharedPreferences.getBoolean(SHARED_KEY_SETTING_GROUPS_SYNCED,
                false);
    }

    public void setContactSynced(boolean synced) {
        editor.putBoolean(SHARED_KEY_SETTING_CONTACT_SYNCED, synced);
        editor.commit();
    }

    public boolean isContactSynced() {
        return mSharedPreferences.getBoolean(SHARED_KEY_SETTING_CONTACT_SYNCED,
                false);
    }

    public void setBlacklistSynced(boolean synced) {
        editor.putBoolean(SHARED_KEY_SETTING_BALCKLIST_SYNCED, synced);
        editor.commit();
    }

    public boolean isBacklistSynced() {
        return mSharedPreferences
                .getBoolean(SHARED_KEY_SETTING_BALCKLIST_SYNCED, false);
    }

    public void setCurrentUserNick(String nick) {
        editor.putString(SHARED_KEY_CURRENTUSER_NICK, nick);
        editor.commit();
    }

    public void setCurrentUserAvatar(String avatar) {
        editor.putString(SHARED_KEY_CURRENTUSER_AVATAR, avatar);
        editor.commit();
    }

    public String getCurrentUserNick() {
        return mSharedPreferences.getString(SHARED_KEY_CURRENTUSER_NICK, null);
    }

    public void setCurrentUserBackground(String background) {
        editor.putString("SHARED_KEY_CURRENTUSER_BACKGROUND", background);
        editor.commit();
    }

    public String getCurrentUserBackground() {
        return mSharedPreferences.getString("SHARED_KEY_CURRENTUSER_BACKGROUND",
                null);
    }

    public void setCurrentUserSex(String sex) {
        editor.putString("SHARED_KEY_CURRENTUSER_SEX", sex);
        editor.commit();
    }

    public String getCurrentUserSex() {
        return mSharedPreferences.getString("SHARED_KEY_CURRENTUSER_SEX", null);
    }

    public void setCurrentUserSignature(String Signature) {
        editor.putString("SHARED_KEY_CURRENTUSER_Signature", Signature);
        editor.commit();
    }

    public String getCurrentUserSignature() {
        return mSharedPreferences.getString("SHARED_KEY_CURRENTUSER_Signature",
                null);
    }

    public String getCurrentUserAvatar() {
        return mSharedPreferences.getString(SHARED_KEY_CURRENTUSER_AVATAR,
                null);
    }

    public void setCurrentUserMobile(String mobile) {
        editor.putString("SHARED_KEY_CURRENTUSER_Mobile", mobile);
        editor.commit();
    }

    public String getCurrentUserMobile() {
        return mSharedPreferences.getString("SHARED_KEY_CURRENTUSER_Mobile",
                null);
    }

    public void setCurrentUserONE(String Signature) {
        editor.putString(ONE, Signature);
        editor.commit();
    }

    public String getCurrentUserONE() {
        return mSharedPreferences.getString(ONE, null);
    }

    public void setCurrentUserTwo(String Signature) {
        editor.putString(TWO, Signature);
        editor.commit();
    }

    public String getCurrentUserTwo() {
        return mSharedPreferences.getString(TWO, null);
    }

    public void setCurrentUserThree(String Signature) {
        editor.putString(THREE, Signature);
        editor.commit();
    }

    public String getCurrentUserThree() {
        return mSharedPreferences.getString(THREE, null);
    }

    public void setCurrentUserThreeLogin(int threeLogin) {
        editor.putInt("SHARED_KEY_CURRENTUSER_THREE_LOGIN", threeLogin);
        editor.commit();
    }

    public int getCurrentUserThreeLogin() {
        return mSharedPreferences.getInt("SHARED_KEY_CURRENTUSER_THREE_LOGIN",
                0);
    }

    public void setDownloadServiceSuccess(String filename) {
        editor.putString(DOWNLOADSERVICE_SUCCESS, filename);
        editor.commit();
    }

    public String getDownloadServiceSuccess() {
        return mSharedPreferences.getString(DOWNLOADSERVICE_SUCCESS, null);
    }

    public void setDownloadServicePath(String path) {
        editor.putString(DOWNLOADSERVICE_PATH, path);
        editor.commit();
    }

    public String getDownloadServicePath() {
        return mSharedPreferences.getString(DOWNLOADSERVICE_PATH, null);
    }

    public void removeCurrent() {
        editor.putString(ONE, null);
        editor.putString(TWO, null);
        editor.putString(THREE, null);
        editor.commit();
    }

    public void removeCurrentUserInfo() {
        editor.remove(SHARED_KEY_CURRENTUSER_NICK);
        editor.remove(SHARED_KEY_CURRENTUSER_AVATAR);
        editor.remove("SHARED_KEY_CURRENTUSER_Signature");
        editor.remove("SHARED_KEY_CURRENTUSER_SEX");
        editor.remove("SHARED_KEY_CURRENTUSER_BACKGROUND");
        editor.remove("SHARED_KEY_CURRENTUSER_Mobile");
        editor.remove("SHARED_KEY_CURRENTUSER_THREE_LOGIN");
        editor.commit();
    }
}
